package com.example.pruebaHttp;


import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.datas.Comunicador;
import com.example.datas.Constants;
import com.example.datas.SummonerData;

public class RankedStatsActivity extends Activity{


	private String[] splashURL = {"","",""};
	private Bitmap splashBM0;
	private Bitmap splashBM1;
	private Bitmap splashBM2;
	private ImageView mostUsed0;
	private ImageView mostUsed1;
	private ImageView mostUsed2;
	@SuppressWarnings("deprecation")
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		LayoutParams parms;
		Display display = getWindowManager().getDefaultDisplay();
		SummonerData summonerData = Comunicador.getCommunicador();
		if(summonerData.getSummonerRank().equals("UNRANKED")){
			setContentView(R.layout.ranked_data_unranked);

		}else{
			setContentView(R.layout.ranked_data);
		//Scaling Border:
		////Top
		ImageView top = (ImageView) findViewById(R.id.topborder);
		parms = top.getLayoutParams();
		parms.width = display.getWidth();
		parms.height =(int) (/*(24/720)*/0.033 * parms.width);
		top.setLayoutParams(parms);
		////Bot
		ImageView bot = (ImageView) findViewById(R.id.botborder);
		parms = bot.getLayoutParams();
		parms.width = display.getWidth();
		parms.height =(int) (/*(37/720)*/0.051 * parms.width);
		bot.setLayoutParams(parms);
		////left
		ImageView left = (ImageView) findViewById(R.id.leftborder);
		parms = left.getLayoutParams();
		parms.height = display.getHeight();
		parms.width = (int) (/*(23/1262)*/0.018 * parms.height);
		left.setLayoutParams(parms);
		////right
		ImageView right = (ImageView) findViewById(R.id.rightborder);
		parms = right.getLayoutParams();
		parms.height = display.getHeight();
		parms.width = (int) (/*(24/1268)*/0.019 * parms.height);
		right.setLayoutParams(parms);
		//Scaling RankImage (30%Width)
		ImageView rankImage = (ImageView) findViewById(R.id.rankImage);
		parms = rankImage.getLayoutParams();
		parms.width = (int) (0.3 * display.getWidth());
		parms.height = parms.width;
		rankImage.setLayoutParams(parms);
		//Creating data content:
		//SettingMiniSeries;
		if(summonerData.getRankedSeriesProgress()==null){
			LinearLayout series = (LinearLayout) findViewById(R.id.seriesLayout);
			series.setVisibility(View.INVISIBLE);
		}
		else{
			String rankedSeriesProgress = summonerData.getRankedSeriesProgress();
			ImageView[] seriesImages ={((ImageView) findViewById(R.id.serie0)),((ImageView) findViewById(R.id.serie1)),((ImageView) findViewById(R.id.serie2))};
			for(int i = 0;i<3;i++){
				if(rankedSeriesProgress.charAt(i)=='L'){
					seriesImages[i].setImageDrawable(getResources().getDrawable(R.drawable.miniseriesloss));
				}
				else if(rankedSeriesProgress.charAt(i)=='W'){
					seriesImages[i].setImageDrawable(getResources().getDrawable(R.drawable.miniserieswin));

				}
			}
		}
		//SEtting League points:
		((TextView) findViewById(R.id.leaguePoints)).setText(""+summonerData.getRankedLeaguePoints());
		//Setting League Name:
		TextView rank = (TextView) findViewById(R.id.summonerLeague);
		((TextView) findViewById(R.id.summonerLeagueName)).setText(""+summonerData.getLeagueName());
		
		if (summonerData.getSummonerRank().contains("BRONZE")){
			rank.setTextColor(Color.parseColor("#8A2908"));
			rankImage.setImageDrawable(getResources().getDrawable(R.drawable.bronze_6));

		}
			
		if (summonerData.getSummonerRank().contains("SILVER")){
			rank.setTextColor(Color.parseColor("#848484"));
			rankImage.setImageDrawable(getResources().getDrawable(R.drawable.silver_6));


		}
			
		if (summonerData.getSummonerRank().contains("GOLD")){
			rank.setTextColor(Color.parseColor("#D7DF01"));
			rankImage.setImageDrawable(getResources().getDrawable(R.drawable.gold_6));


		}
			
		if (summonerData.getSummonerRank().contains("PLATINUM")){
			rank.setTextColor(Color.parseColor("#04B486"));
			rankImage.setImageDrawable(getResources().getDrawable(R.drawable.platinum_6));


		}
			
		if (summonerData.getSummonerRank().contains("DIAMOND")){
			rank.setTextColor(Color.parseColor("#2EFEF7"));
			rankImage.setImageDrawable(getResources().getDrawable(R.drawable.diamond_6));


		}
			
		if (summonerData.getSummonerRank().contains("CHALLENGER")){
			rank.setTextColor(Color.parseColor("#D7DF01"));
			rank.setShadowLayer(1.5f, 1, 2, Color.parseColor("#2EFEF7"));
			rankImage.setImageDrawable(getResources().getDrawable(R.drawable.challenger_1));

		}
		
		rank.setText(""+summonerData.getSummonerRank());
		
		//Setting MostUsedChamps.
		for(int i =0;i<3;i++){
			splashURL[i]="http://ddragon.leagueoflegends.com/cdn/"
					+ summonerData.getDDVersion() + "/img/champion/"
					+ Constants.getChampionName(summonerData.getMostPlayedChamps().get(i).getId()) + ".png";
		}
		mostUsed0=(ImageView) findViewById(R.id.mostUsedChamp0);
		mostUsed1=(ImageView) findViewById(R.id.mostUsedChamp1);
		mostUsed2=(ImageView) findViewById(R.id.mostUsed2);

		TareaWSConsulta tarea = new TareaWSConsulta();
		tarea.execute();
		((TextView) findViewById(R.id.rankedWins)).setText(""+summonerData.getRankedWins());
		((TextView) findViewById(R.id.rankedLost)).setText(""+summonerData.getRankedLosses());
		TextView dk = (TextView) findViewById(R.id.totalDK);
		dk.setText(""+dk.getText()+summonerData.getRankedDK());
		TextView tk = (TextView) findViewById(R.id.totalTK);
		tk.setText(""+tk.getText()+summonerData.getRankedTK());
		TextView qk = (TextView) findViewById(R.id.totalQK);
		qk.setText(""+qk.getText()+summonerData.getRankedQK());
		TextView pk = (TextView) findViewById(R.id.totalPK);
		pk.setText(""+pk.getText()+summonerData.getRankedPK());
		TextView cs = (TextView) findViewById(R.id.rankedAverageCS);
		cs.setText(""+cs.getText()+summonerData.getRankedAverageCS());
		TextView turrets = (TextView) findViewById(R.id.totalTurretsKilled);
		turrets.setText(""+turrets.getText()+summonerData.getRankedTotalTurretsKilleds());
		TextView crit = (TextView) findViewById(R.id.rankedBestCrit);
		crit.setText(""+crit.getText()+summonerData.getRankedLargestCritStrike());
	
		((TextView) findViewById(R.id.mostUsedKDA0)).setText(" "+summonerData.getMostPlayedChamps().get(0).getStats().getTotalChampionKills()
				+"/"+summonerData.getMostPlayedChamps().get(0).getStats().getTotalDeathsPerSession()
				+"/"+summonerData.getMostPlayedChamps().get(0).getStats().getTotalAssists()+" ");
		((TextView) findViewById(R.id.mostUsedKDA1)).setText(" "+summonerData.getMostPlayedChamps().get(1).getStats().getTotalChampionKills()
				+"/"+summonerData.getMostPlayedChamps().get(1).getStats().getTotalDeathsPerSession()
				+"/"+summonerData.getMostPlayedChamps().get(1).getStats().getTotalAssists()+" ");
		((TextView) findViewById(R.id.mostUsedKDA2)).setText(" "+summonerData.getMostPlayedChamps().get(2).getStats().getTotalChampionKills()
				+"/"+summonerData.getMostPlayedChamps().get(2).getStats().getTotalDeathsPerSession()
				+"/"+summonerData.getMostPlayedChamps().get(2).getStats().getTotalAssists()+" ");
	
		((TextView) findViewById(R.id.mostUsedW0)).setText(""+summonerData.getMostPlayedChamps().get(0).getStats().getTotalSessionsWon());
		((TextView) findViewById(R.id.mostUsedL0)).setText(""+summonerData.getMostPlayedChamps().get(0).getStats().getTotalSessionsLost());
		((TextView) findViewById(R.id.mostUsedW1)).setText(""+summonerData.getMostPlayedChamps().get(1).getStats().getTotalSessionsWon());
		((TextView) findViewById(R.id.mostUsedL1)).setText(""+summonerData.getMostPlayedChamps().get(1).getStats().getTotalSessionsLost());
		((TextView) findViewById(R.id.mostUsedW2)).setText(""+summonerData.getMostPlayedChamps().get(2).getStats().getTotalSessionsWon());
		((TextView) findViewById(R.id.mostUsedL2)).setText(""+summonerData.getMostPlayedChamps().get(2).getStats().getTotalSessionsLost());
		}
		ImageView bg = (ImageView) findViewById(R.id.rankedbg);
		parms = bg.getLayoutParams();
		parms.height = display.getHeight();
		parms.width = display.getWidth();
		bg.setLayoutParams(parms);
	}
	private class TareaWSConsulta extends AsyncTask<String, Integer, Boolean> {

		protected Boolean doInBackground(String... params) {
			boolean resul = true;
			try {
					splashBM0 = BitmapFactory
							.decodeStream((InputStream) new URL(splashURL[0])
									.getContent());
					splashBM1 = BitmapFactory
							.decodeStream((InputStream) new URL(splashURL[1])
									.getContent());
					splashBM2 = BitmapFactory
							.decodeStream((InputStream) new URL(splashURL[2])
									.getContent());

			} catch (Exception e) {
				e.printStackTrace();
			}

			return resul;
		}

		protected void onPostExecute(Boolean result) {

				mostUsed0.setImageBitmap(splashBM0);
				mostUsed1.setImageBitmap(splashBM1);
				mostUsed2.setImageBitmap(splashBM2);
			
		}

	}

}
